//
//  DeviceInfo.swift
//  Kosspot
//
//  Created by 胡辉 on 2025/7/13.
//

import Foundation
import UTEBluetoothRYApi

struct DeviceInfo: Codable,Equatable,Hashable {
    var name: String
    var identifier: String?
    var advertisementAddress: String?
    var isConnected: Bool?
    var isPair: Bool?
    var rssi: Int?
    var isUpdateMode: Bool?
    var mServicesUUID: [String]?
    var mServicesConnectedUUID: [String]?
    var maxBluetooth5DataLength: Int?
    var version: String?
    var versionName: String?
    var versionNum: String?
    var battery: Int?
    var address: Data?
    var addressStr: String?
        
    init(from ocDevice: UTEModelDevice) {
        self.name = ocDevice.name ?? ""
        self.identifier = ocDevice.identifier
        self.advertisementAddress = ocDevice.advertisementAddress
        self.isConnected = ocDevice.isConnected
        self.isPair = ocDevice.isPair
        self.rssi = ocDevice.rssi
        self.isUpdateMode = ocDevice.isUpdateMode
        self.mServicesUUID = (ocDevice.mServicesUUID as? [Any])?.compactMap { "\($0)" } ?? []
        self.mServicesConnectedUUID = (ocDevice.mServicesConnectedUUID as? [Any])?.compactMap { "\($0)" } ?? []
        self.maxBluetooth5DataLength = ocDevice.maxBluetooth5DataLength
        self.battery = ocDevice.battery
        self.version = ocDevice.version
        self.versionName = ocDevice.versionName
        self.versionNum = ocDevice.versionNum
        self.address = ocDevice.address
        self.addressStr = ocDevice.addressStr
    }
    
    func toUTEModelDevice() -> UTEModelDevice {
        let device = UTEModelDevice()
        device.name = self.name
        device.identifier = self.identifier
        device.advertisementAddress = self.advertisementAddress
        device.isConnected = self.isConnected ?? false
        device.isPair = self.isPair ?? false
        device.rssi = self.rssi ?? 0
        device.isUpdateMode = self.isUpdateMode ?? false
        device.mServicesUUID = self.mServicesUUID.map { NSMutableArray(array: $0) }
        device.mServicesConnectedUUID = self.mServicesConnectedUUID.map { NSMutableArray(array: $0) }
        device.maxBluetooth5DataLength = self.maxBluetooth5DataLength ?? 0
        device.version = self.version
        device.versionName = self.versionName
        device.versionNum = self.versionNum
        device.battery = self.battery ?? 0
        device.address = self.address
        device.addressStr = self.addressStr
        return device
    }
    
}

